home *** CD-ROM | disk | FTP | other *** search
- /* Main.c */
- /* Convert PICT files to EPSF. */
- /* Copyright 1992, Gary D. McGath */
-
- /* Mac Resource Defs */
-
- #define APPLMENU 128
- #define FILEMENU 180
- #define WatchCursor 4
- #define NIL 0L
-
- extern void DrawEPSF(PicHandle han, int theFile);
-
- int main (void);
- void mainloop(void);
- void docommand(long mensel);
- void doconvert(void);
- PicHandle ReadPict(int theFile);
-
-
- extern int thePSResFile;
-
- MenuHandle apmenu;
- MenuHandle filemenu;
- EventRecord myevent;
- Handle pictHan;
- WindowPtr whichwindow;
- int infile;
- int thePSFile;
- Point gfloc = {40,40};
- SFTypeList typelist = {'PICT'}; /* list of acceptable file types */
-
- main()
- {
-
- InitGraf((Ptr)&thePort);
- InitFonts();
- InitWindows();
- InitDialogs(0L);
- InitMenus();
- InitCursor();
-
- apmenu = GetMenu(APPLMENU);
- AddResMenu(apmenu,'DRVR');
- InsertMenu(apmenu,0);
- filemenu = GetMenu(FILEMENU);
- InsertMenu(filemenu,0);
- DrawMenuBar();
- mainloop();
- }
-
-
- void mainloop()
- {
- int code;
- int ch;
-
- for (;;) {
- WaitNextEvent(everyEvent,&myevent,2L,NIL);
- switch (myevent.what) {
- case keyDown:
- ch = myevent.message & charCodeMask;
- if (myevent.modifiers & cmdKey)
- docommand(MenuKey(ch));
- break;
- case mouseDown:
- code = FindWindow(myevent.where,&whichwindow);
- switch (code) {
- case inMenuBar:
- docommand(MenuSelect(myevent.where));
- break;
- case inSysWindow:
- SystemClick(&myevent,whichwindow);
- break;
- }
- break;
- }
- }
- }
-
-
-
- void docommand(long mensel)
- {
- register short men, item;
- char accname[64]; /* holder for desk accessory name */
- GrafPtr saveport;
-
- men = mensel >> 16; /* high order word is menu */
- item = (short) mensel; /* low order word is item */
- if (men == 0)
- return; /* nothing to do */
- if (men == APPLMENU) {
- if (item <= 2) /* "about" -- not implemented */
- SysBeep(1);
- else {
- GetItem(apmenu,item,(StringPtr)accname);
- GetPort(&saveport); /* don't let desk acc bomb the port */
- OpenDeskAcc((StringPtr)accname);
- SetPort(saveport);
- }
- }
- else switch(item) { /* only other menu is File */
- case 1:
- doconvert(); /* to resource */
- break;
- case 3 :
- default:
- ExitToShell();
- }
- HiliteMenu(0); /* clean up display */
- }
-
-
-
- /* Command handler to convert PICT to EPSF. */
- void doconvert()
- {
- SFReply myreply; /* reply structure from open dialog */
- unsigned char *pt;
- OSErr errcod;
- PicHandle picHan;
-
- /* specify and open input (PICT) file */
- SFGetFile(gfloc,0L,0L,1,&typelist,0L,&myreply);
- if (!myreply.good)
- return; /* cancelled out */
- errcod = FSOpen(&myreply.fName[0],myreply.vRefNum,&infile);
- if (errcod != noErr)
- return;
-
- /* specify output file */
- for (pt = &myreply.fName[1]; pt < &myreply.fName[32];)
- *pt++ = 0; /* clear out name buffer */
- SFPutFile(gfloc,(StringPtr)"\pName of output file:",(StringPtr)"\pEPSF File",0L,&myreply);
- if (!myreply.good) {
- FSClose(infile);
- return; /* cancelled out */
- }
- SetCursor(*GetCursor(WatchCursor)); /* Go get lunch now */
- errcod = CreateEPSFFile(&myreply, &thePSFile);
- if (errcod == noErr) {
- picHan = ReadPict(infile);
- if (picHan == 0)
- goto done;
- AddResource(picHan,'PICT',256,"\p");
- DrawEPSF(picHan, thePSFile); /* generate the PostScript */
- }
-
- done:
- if (infile)
- FSClose(infile);
- if (thePSResFile)
- CloseResFile(thePSResFile);
- if (thePSFile)
- FSClose(thePSFile);
- FlushVol(0,0);
- InitCursor();
- }
-
-
-
- /* Read the PICT into a PicHandle */
- PicHandle ReadPict(int theFile)
- {
- long eofPos;
- long count;
- Handle theHan;
- SetFPos(theFile, fsFromStart, 512L); /* skip PICT header */
- GetEOF(theFile, &eofPos); /* get file length */
- count = eofPos - 512;
- theHan = NewHandle(count); /* try to allocate */
- if (MemError())
- return 0;
- HLock(theHan);
- FSRead(theFile, &count, *theHan); /* read it in */
- HUnlock(theHan);
- return ((PicHandle) theHan);
- }
-